package com.yupi.yuoj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuoj.common.BaseResponse;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.common.ResultUtils;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.question.QuestionQueryRequest;
import com.yupi.yuoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.yupi.yuoj.model.entity.Question;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.model.vo.QuestionDoStatusVO;
import com.yupi.yuoj.model.vo.QuestionLanguageStatus;
import com.yupi.yuoj.model.vo.QuestionVO;
import com.yupi.yuoj.service.QuestionSubmitService;
import com.yupi.yuoj.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * @Classname QuestionSubmitController
 * @Description 提交题目接口
 * @Date 2025/8/27 16:24
 * @Created by 李胜
 */
@RestController
@Slf4j
@RequestMapping("/question_submit")
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest, HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交
        User loginUser = userService.getLoginUser(request);
        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(questionSubmitId);
    }

    /**
     * 查询用户的做题情况
     * @param request
     * @return
     */
    @GetMapping("/statics/pass")
    public BaseResponse<QuestionDoStatusVO> questionDoStatus(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NOT_LOGIN_ERROR);
        long questionDoStatusAC = questionSubmitService.questionDoStatusAC(loginUser.getId());
        long questionDoStatusALL = questionSubmitService.questionDoStatusALL(loginUser.getId());
        QuestionDoStatusVO questionDoStatusVO = new QuestionDoStatusVO();
        questionDoStatusVO.setAcProblemNum(questionDoStatusAC);
        questionDoStatusVO.setSubmitProblemNum(questionDoStatusALL);
        return ResultUtils.success(questionDoStatusVO);
    }

    /**
     * 统计不同语言的过题数
     * @param request
     * @return
     */
    @GetMapping("/statics/language")
    public BaseResponse<List<QuestionLanguageStatus>> questionLanguageStatus(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(questionSubmitService.questionLanguageStatus(loginUser.getId()));
    }
}
